<?php
    /* functions */
    function serviceTeamobi($info)
    {        
        $url = "http://123.30.174.212/sms/taoviec/sms.php?username=taoviec&password=hjhjhuty123";    
        $url .= $info;
        
        $response = cUrl($url);
        $response = explode("|", $response);
        
        $res = array();
        $res["smstype"] = isset($response[0]) ? $response[0] : "" ;
        $res["url"] = isset($response[1]) ? $response[1] : "" ;
        $res["status"] = isset($response[2]) ? $response[2] : "" ;
        $res["msg"] = isset($response[3]) ? $response[3] : "" ;
        
        return $res;
    }
    
    function buildSyntax($content, $regx)
    {
        preg_match($regx, $content, $match);        
        $userId = !empty($match[2]) ? $match[2] : 0;        
        $username = !empty($match[5]) ? $match[5] : "";        
        
        if(preg_match("/^[a-zA-Z]{1,}$/", $userId)){
            $userId = DecodeTeamobi($userId);
        }
        /* 
        * Kiểm tra user có tồn tại không
        * Không tồn tại thì trả về 0
        */
        
        $user = getUserById($userId);
        
        $result = array();
        $result["userId"] = !empty($user) ? $user["id"] : 0;
        $result["user_name"] = !empty($user) ? $user["username"] : "";
        $result["username"] = $username;
        
        return $result;
    }
    
    function DecodeTeamobi($code)
    {
        $codes = array(0=>'A', 1=>'B', 2=>'C', 3=>'D', 4=>'E', 5=>'F', 6=>'G', 7=>'H', 8=>'I', 9=>'J', 10=>'K', 11=>'L', 12=>'M', 13=>'N', 14=>'O', 15=>'P', 16=>'Q', 17=>'R', 18=>'S', 19=>'T', 20=>'U', 21=>'V', 22=>'W', 23=>'X', 24=>'Y', 25=>'Z', 26=>'a', 27=>'b', 28=>'c', 29=>'d', 30=>'e', 31=>'f', 32=>'g', 33=>'h', 34=>'i', 35=>'j', 36=>'k', 37=>'l', 38=>'m', 39=>'n', 40=>'o', 41=>'p', 42=>'q', 43=>'r', 44=>'s', 45=>'t', 46=>'u', 47=>'v', 48=>'w', 49=>'x', 50=>'y', 51=>'z');
        if(strlen($code)==1){
            $id = array_search($code, $codes);    
        } else if(strlen($code)==2){
            $hc = substr($code, 0, 1);    
            $hdv = substr($code, 1, 1);    
            
            $id = array_search($hc, $codes) * 52 + array_search($hdv, $codes);
        } else if(strlen($code)==3){
            $ht = substr($code, 0, 1);    
            $hc = substr($code, 1, 1);    
            $hdv = substr($code, 2, 1);
            
            $id = array_search($ht, $codes) * 52 * 52 + array_search($hc, $codes) * 52 + array_search($hdv, $codes);    
        } else {
            $id = 0;
        }
        
        return $id;
    }
    
    /*
    * processing
    */
    
    function processing($data)
    {
        $regx_reg_kpah = "/^TVTM(\s+)REF([a-zA-Z0-9]{1,})(\s+)RE9(\s+)([a-zA-Z0-9_]{1,})(\s+)([a-zA-Z0-9_.]{1,})(.*)$/si";
        $regx_reg_avatar = "/^TVTM(\s+)REF([a-zA-Z0-9]{1,})(\s+)RE8(\s+)([a-zA-Z0-9_]{1,})(\s+)([a-zA-Z0-9_.]{1,})(.*)$/si";
        $regx_reg_mobiarmy = "/^TVTM(\s+)REF([a-zA-Z0-9]{1,})(\s+)RE4(\s+)([a-zA-Z0-9_]{1,})(\s+)([a-zA-Z0-9_.]{1,})(.*)$/si";        
        
        $regx_luong_avatar = "/^TVTM(\s+)REF([a-zA-Z0-9]{1,})(\s+)(LUONG7|LUONG8)(\s+)([a-zA-Z0-9_]{1,})$/si";
        $regx_luong_mobiarmy = "/^TVTM(\s+)REF([a-zA-Z0-9]{1,})(\s+)LUONG5(\s+)([a-zA-Z0-9_]{1,})$/si";
        
        $regx_nap_avatar = "/^TVTM(\s+)REF([a-zA-Z0-9]{1,})(\s+)(NAP7|NAP8)(\s+)([a-zA-Z0-9_]{1,})$/si";
        $regx_nap_mobiarmy = "/^TVTM(\s+)REF([a-zA-Z0-9]{1,})(\s+)NAP5(\s+)([a-zA-Z0-9_]{1,})$/si";
        
        $data_reg = array();
        if(
            preg_match($regx_reg_avatar, $data["content"])
             || preg_match($regx_reg_mobiarmy, $data["content"])
             || preg_match($regx_luong_avatar, $data["content"])
             || preg_match($regx_luong_mobiarmy, $data["content"]) 
             || preg_match($regx_nap_avatar, $data["content"]) 
             || preg_match($regx_nap_mobiarmy, $data["content"])        
             || preg_match($regx_reg_kpah, $data["content"])        
        ){
            
            if(preg_match($regx_reg_avatar, $data["content"])){
                
                /* Đăng ký user */
                $syntax = buildSyntax($data["content"], $regx_reg_avatar);
                $list_service_code = array("6522");
                $data["id"] = 147; //ID game
                $data["action"] = "reg";
                
            } else if(preg_match($regx_reg_mobiarmy, $data["content"])){
                
                /* Đăng ký user */
                $syntax = buildSyntax($data["content"], $regx_reg_mobiarmy);
                $list_service_code = array("6522");
                $data["id"] = 146; //ID game
                $data["action"] = "reg";
                
            } else if(preg_match($regx_luong_avatar, $data["content"])){
                
                /* nạp lượng */
                $syntax = buildSyntax($data["content"], $regx_luong_avatar);
                $list_service_code = array("6722");
                $data["id"] = 147; //ID game
                $data["action"] = "nap";
                
            } else if(preg_match($regx_luong_mobiarmy, $data["content"])){
                
                /* nạp lượng */
                $syntax = buildSyntax($data["content"], $regx_luong_mobiarmy);
                $list_service_code = array("6722");
                $data["id"] = 146; //ID game
                $data["action"] = "nap";
                
            } else if(preg_match($regx_nap_avatar, $data["content"])){
                
                /* nạp xu */
                $syntax = buildSyntax($data["content"], $regx_nap_avatar);
                $list_service_code = array("6622", "6722");
                $data["id"] = 147; //ID game
                $data["action"] = "nap";
                
            } else if(preg_match($regx_nap_mobiarmy, $data["content"])){
                
                /* nạp xu */
                $syntax = buildSyntax($data["content"], $regx_nap_mobiarmy);
                $list_service_code = array("6622", "6722");
                $data["id"] = 146; //ID game
                $data["action"] = "nap";
                
            } else if(preg_match($regx_reg_kpah, $data["content"])){
                
                /* nạp xu */
                $syntax = buildSyntax($data["content"], $regx_reg_kpah);
                $list_service_code = array("6522");
                $data["id"] = 158; //ID game
                $data["action"] = "reg";
                
            }
            
            $data_reg["game_user"] = isset($syntax["username"]) ? $syntax["username"] : "";
            $data_reg["username"] = isset($syntax["user_name"]) ? $syntax["user_name"] : "";
             
            $data["userId"] = $syntax["userId"];
            if(in_array($data["serviceCode"], $list_service_code)){
                                        
                $info = "&src=" . $data["sender"] . "&dest=" . $data["serviceCode"] . "&smsbody=" . urlencode($data["content"]) . "&smsid=" . $data["transactionId"];

                $refTeamobi = serviceTeamobi($info);                        

                $data["status"] = $refTeamobi["status"]==1 ? 1 : 0;
                $response["status"] = $refTeamobi["status"];
                $response["msg"] = $refTeamobi["msg"];
                
            } else {
                $response["status"] = 3;                        
                $response["msg"] = "Tin nhan khong dung dau so quy dinh";                
                
                $data["status"] = 0;                 
            }               
                
        } else {
            $response["status"] = 2;                
            $response["msg"] = "Tin nhan sai cu phap";                
            
            $data["userId"] = 0;                
            $data["status"] = 0;    
            
            $data["id"] = 0; //ID game
            $data["action"] = null;
        }
        
        /* log sms */
        $data["telco"] = checkTelco($data["sender"]);    
        $data["price"] = priceServiceCode($data["serviceCode"]);    
        $data["partnerId"] = 54; //ID partner Teamobi    
        $data["type"] = 1; //Thể loại game   
        
        $data["output"] = $response["status"] . "|" . $data["transactionId"] . "|" . $response["msg"];    
        
        /* tỷ lệ ăn chia */
        $percent = getPercent($data["id"]);
        
        $data["percent_wapmaster"] = isset($percent["percent_wapmaster"]) ? $percent["percent_wapmaster"] : 0;
        $data["percent_partner"] = isset($percent["percent_partner"]) ? $percent["percent_partner"] : 0;
        
        $result = InsertSmsLog($data);   
        
        if($data["action"]=="reg"){
            $data_reg["userId"] = $data["userId"];    
            $data_reg["gameId"] = $data["id"];                
            
            /* Check xem user da ton tai chua */
            $result = CheckGameUser($data_reg);
            if($result==0){
                $result = InsertGameUser($data_reg);    
            }            
        }
        
        return $response;
    }
?>
